//
//  MyLoginBtn.swift
//  LoginButton
//
//  Created by linktoplinktop on 2017/3/14.
//  Copyright © 2017年 None. All rights reserved.
//

import UIKit

class MyLoginBtn: UIView, CAAnimationDelegate {
    
    
    
    private var _oriX:CGFloat = 0
    private var _oriY:CGFloat = 0
    private var _width:CGFloat = 0
    private var _height:CGFloat = 0

    
    private let _FreshBlue:UIColor = UIColor(red: 25.0 / 255.0, green: 155.0 / 255.0, blue: 200.0 / 255.0, alpha: 1.0)
    private let _FreshGreen:UIColor = UIColor(red: 150.0 / 255.0, green: 203.0 / 255.0, blue: 25.0 / 255.0, alpha: 1.0) //Color
    
    private var _contentView:UIView? //内容
    private var _borderView:UIView?  //边框
    
    private var _lblText:UILabel?
    

    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        _oriX = frame.origin.x;
        _oriY = frame.origin.y;
        _width = frame.size.width;
        _height = frame.size.height;
        
        _contentView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: frame.size.width, height: frame.size.height));
        _contentView?.backgroundColor = _FreshBlue;

        _borderView = UIView.init(frame: (_contentView?.frame)!);
        _borderView?.backgroundColor = UIColor.clear;
        _borderView?.layer.borderColor = _FreshBlue.cgColor;
        _borderView?.layer.borderWidth = 3.0;
        
        self.addSubview(_contentView!);
        self.addSubview(_borderView!);
        
        //文字
        _lblText = UILabel.init(frame: (_contentView?.frame)!);
        _lblText?.text = "upload";
        _lblText?.textAlignment = NSTextAlignment.center;
        _lblText?.textColor = UIColor.white;
        _lblText?.font = UIFont.systemFont(ofSize: 20.0)
        self.addSubview(_lblText!);
        
        
    }
    
    
    //MARK: 动画相关方法
    
    
    /// 收缩动画
    func startAnimate() {
        _lblText?.removeFromSuperview();
        
        //变圆动画
        let roundAnimate = CABasicAnimation();
        roundAnimate.keyPath = "cornerRadius"
        roundAnimate.fromValue = 5.0
        roundAnimate.toValue = _height / 2.0
        
        //缩放
        let boundAnimate = CABasicAnimation();
        boundAnimate.keyPath = "bounds"
        boundAnimate.toValue = NSValue.init(cgRect: CGRect.init(x: _oriX + _width/2 - _height,
                                                                y: _oriY,
                                                                width: _height,
                                                                height: _height));
        //透明度
        let opacityAnimate:CABasicAnimation = CABasicAnimation();
        opacityAnimate.keyPath = "opacity";
        opacityAnimate.toValue = 0;
        
        //边框颜色
        let borderColorAnimate :CABasicAnimation = CABasicAnimation();
        borderColorAnimate.keyPath = "borderColor"
        borderColorAnimate.toValue = UIColor.init(red: 224.0/255, green: 224.0/255, blue: 224.0/255, alpha: 1.0).cgColor;
        
        //动画组
        let dura = 1.0;
        
        let animaGroup : CAAnimationGroup = CAAnimationGroup();
        animaGroup.duration = dura;
        animaGroup.repeatCount = 1;
        animaGroup.fillMode = kCAFillModeForwards;
        animaGroup.isRemovedOnCompletion = false;
        animaGroup.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut);
        animaGroup.animations = [roundAnimate, boundAnimate, opacityAnimate];
        
        
        let animaGroupBoard :CAAnimationGroup = CAAnimationGroup();
        animaGroupBoard.duration = dura;
        animaGroupBoard.repeatCount = 1;
        animaGroupBoard.fillMode = kCAFillModeForwards;
        animaGroupBoard.isRemovedOnCompletion = false;
        animaGroupBoard.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut);
        
        animaGroupBoard.animations = [roundAnimate, boundAnimate, borderColorAnimate];
        animaGroupBoard.delegate = self;
        animaGroupBoard.setValue("myAniamteBoard", forKey: "animateOne");
        
        //动画提交
        CATransaction.begin()
        
        _contentView?.layer.add(animaGroup, forKey: "myAniamteDispear");

        _borderView?.layer.add(animaGroupBoard, forKey: "myAniamteBoard");
    
        CATransaction.commit();
    }
    
    //MARK: 动画代理方法
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        
        
        
    }
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    

}
